package com.stylefeng.guns.rest.modular.users.service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.stylefeng.guns.rest.common.persistence.dao.SmsCodeMapper;
import com.stylefeng.guns.rest.common.persistence.dao.UserMapper;
import com.stylefeng.guns.rest.common.persistence.model.User;
import com.stylefeng.guns.rest.common.utils.Log;
import com.stylefeng.guns.rest.modular.attent.controller.dto.AttentUserResponse;
import com.stylefeng.guns.rest.modular.auth.controller.dto.AuthRequest;
import com.stylefeng.guns.rest.modular.keyword.controller.dto.AppUser;
import com.stylefeng.guns.rest.modular.keyword.controller.dto.KeywordRequestBody;
import com.stylefeng.guns.rest.modular.users.controller.dto.UserRegisterParam;
import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by ASUS-PC on 2018/6/22.
 */
@Service
public class UserService {
    private static Log logger = new Log(LoggerFactory.getLogger(UserService.class));
    @Autowired
    UserMapper userMapper;
    @Autowired
    SmsCodeMapper smsCodeMapper;

    /**
     * 手机号是否已存在
     * @param phone
     * @return
     */
    public int checkUserIsRegiset(String phone){
        List<User> userList = userMapper.selectList(Condition.create().eq("phone" , phone));
        if(userList!=null && userList.size()>0){
            return 1;
        }
        return 0;
    }

    /**
     * 是否有该账号
     * @param account
     * @return
     */
    public boolean hasAccount(String account){
        return userMapper.selectCount(Condition.create().eq("phone" , account)) >= 1 ? true : false;
    }

    //插入用户
    public boolean inserUser(UserRegisterParam request){
        User user = new User();
        user.setPhone(request.getPhone());
        user.setCountryCode(request.getCountryCode());
        if(request.getPhone().length()>6){
            user.setNickName("vlog"+request.getPhone().substring(6,request.getPhone().length()));
        }else {
            user.setNickName("vlog"+request.getPhone());
        }
        user.setPassword(request.getPassword());
        return userMapper.insert(user)>0 ? true:false;
    }

    //用户登陆
    public User login(AuthRequest authRequest){
        /*Map<String,Object> data = new HashedMap();
        data.put("phone",authRequest.getUserName());
        data.put("password",authRequest.getPassword());*/
        List<User> userList = userMapper.selectList(Condition.create().eq("phone" , authRequest.getUserName())
                                                                        .eq("password",authRequest.getPassword()));
        if(userList.size()>0){
            return userList.get(0);
        }
        return null;
    }

    /**
     * 通过id查找用户
     * @param userId
     * @return
     */
    public User selectUserByUserId(int userId){
        List<User> userList = userMapper.selectList(Condition.create().eq("id" , userId));
        return userList.get(0);
    }

    /**
     * 通过用户id更新用户信息
     * @param userId
     * @param user
     * @return
     */
    public boolean updateUserById(int userId , User user){
        return userMapper.update(user , Condition.create().eq("id" , userId)) == 1;
    }

    /**
     * 通过关注数量查询
     * @param
     * @param
     */
    public List<User> findListByCarenum(int page , int size){
        RowBounds rowBounds = new RowBounds(page-1 , size);
        return userMapper.selectPage(rowBounds ,
                Condition.create().orderBy("care_num",false));
    }

    /**
     * 根据关键字查询作者
     * @param request
     * @return
     */
    public List<AttentUserResponse> findUsersByKeyword(KeywordRequestBody request){
        Map<String ,Object> data = new HashedMap();
        data.put("pageIndex" , (request.getPage()-1)*request.getSize());
        data.put("pageSize" , request.getSize());
        data.put("keyword",request.getKeyword());
        return userMapper.findUsersByKeyword(data);
    }

    /**
     * 通过手机号码更新密码
     * @param mobile
     * @param pwd
     */
    public boolean updatePwdByMobile(String mobile , String pwd){
        List<User> userList = userMapper.selectList(Condition.create().eq("phone" , mobile));
        if (userList.size() != 1){
            return false;
        }
        userList.get(0).setPassword(pwd);
        return userMapper.update(userList.get(0) , Condition.create().eq("phone" , mobile)) == 1 ? true : false;
    }

    /**
     * 通过手机号查询用户
     * @param phone
     * @return
     */
    public User checkUserIsRegiset2(String phone){
        List<User> userList = userMapper.selectList(Condition.create().eq("phone" , phone));
        if(userList!=null && userList.size()>0){
            return userList.get(0);
        }
        return null;
    }
}
